package com.wskj.web.controller.system;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.system.bo.SysContactUsAddBo;
import com.wskj.system.bo.SysContactUsEditBo;
import com.wskj.system.bo.SysContactUsQueryBo;
import com.wskj.system.service.ISysContactUsService;
import com.wskj.system.vo.web.SysContactUsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 联系我们Controller
 *
 * @author wsdz
 * @date 2022-12-13
 */
@Api(value = "联系我们控制器", tags = {"联系我们管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/contact")
public class SysContactUsController extends BaseController {

    private final ISysContactUsService iSysContactUsService;

    /**
     * 查询联系我们列表
     */
    @ApiOperation("查询联系我们列表")
    @PreAuthorize("@ss.hasPermi('system:contact:list')")
    @GetMapping("/list")
    public TableDataInfo<SysContactUsVo> list(@Validated SysContactUsQueryBo bo) {
        return iSysContactUsService.queryPageList(bo);
    }

    /**
     * 导出联系我们列表
     */
    @ApiOperation("导出联系我们列表")
    @PreAuthorize("@ss.hasPermi('system:contact:export')")
    @Log(title = "联系我们", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysContactUsQueryBo bo) {
        List<SysContactUsVo> list = iSysContactUsService.queryList(bo);
        ExcelUtil<SysContactUsVo> util = new ExcelUtil<>(SysContactUsVo.class);
        return util.exportExcel(list, "联系我们");
    }

    /**
     * 获取联系我们详细信息
     */
    @ApiOperation("获取联系我们详细信息")
    @PreAuthorize("@ss.hasPermi('system:contact:query')")
    @GetMapping("/{usId}")
    public AjaxResult<SysContactUsVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("usId") Long usId) {
        return AjaxResult.success(iSysContactUsService.queryById(usId));
    }

    /**
     * 新增联系我们
     */
    @ApiOperation("新增联系我们")
    @PreAuthorize("@ss.hasPermi('system:contact:add')")
    @Log(title = "联系我们", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysContactUsAddBo bo) {
        return toAjax(iSysContactUsService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改联系我们
     */
    @ApiOperation("修改联系我们")
    @PreAuthorize("@ss.hasPermi('system:contact:edit')")
    @Log(title = "联系我们", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysContactUsEditBo bo) {
        return toAjax(iSysContactUsService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除联系我们
     */
    @ApiOperation("删除联系我们")
    @PreAuthorize("@ss.hasPermi('system:us:remove')")
    @Log(title = "联系我们" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{usIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] usIds) {
        return toAjax(iSysContactUsService.deleteWithValidByIds(Arrays.asList(usIds), true) ? 1 : 0);
    }

    /**
     * 导入联系我们数据
     */
    @ApiOperation("导入联系我们数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:us:import')")
    @Log(title = "联系我们" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysContactUsVo> util = new ExcelUtil<>(SysContactUsVo.class);
        List<SysContactUsVo> sysContactUsList;
        try {
            sysContactUsList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysContactUsService.importSysContactUs(sysContactUsList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysContactUsVo> util = new ExcelUtil<>(SysContactUsVo.class);
        return util.importTemplateExcel("联系我们数据");
    }
}
